# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=ghostscript
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=10.01.2
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('AGPL' 'custom')
pkgdesc="An interpreter for the PostScript language (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-dbus"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-jbig2dec"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libidn"
         "${MINGW_PACKAGE_PREFIX}-libpaper"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         #"${MINGW_PACKAGE_PREFIX}-lcms2" # Use ghostscript's fork of LCMS2 as it patched to be threadsafe
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gtk3"
             "${MINGW_PACKAGE_PREFIX}-gnutls"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
optdepends=("${MINGW_PACKAGE_PREFIX}-gtk3: needed for gsx")
url="https://www.ghostscript.com/"
source=(https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs${pkgver//./}/ghostscript-${pkgver}.tar.xz
        001-mingw-build.patch
        002-ghostscript-sys-zlib.patch
        003-libspectre.patch
        005-add-arm-arch-config.patch)
sha256sums=('48d079242a2ca02a2e47a76a52cdfa818b2ad769c2bab00ad0497dd13560e7e7'
            '2550617297f1a022e5626fcc86b95ffde90330ebb3302fc8991241fc1dec396c'
            '5cc24c3936fe1670dcfd38dadef21b063f0da1a99a81d796241f594461be9d8b'
            'a1c284d5472e81073ef2f0a445a6238660b9203a7913c315834cac30f34ec146'
            '4749c639175d3b35b16f33639d98f3a8ec3510171303c49b7efaa5015f649cf6')
noextract=(ghostscript-${pkgver}.tar.xz)

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  tar -xJf ${srcdir}/${_realname}-${pkgver}.tar.xz -C ${srcdir} || true

  cd "${srcdir}/${_realname}-${pkgver}"
  _apply_patch_with_msg \
    001-mingw-build.patch \
    002-ghostscript-sys-zlib.patch \
    003-libspectre.patch \
    005-add-arm-arch-config.patch

  sed -e "s|/usr|${MINGW_PREFIX}/${MINGW_CHOST}|g" -i base/all-arch.mak base/unix-aux.mak

  # force it to use system-libs
  rm -rf expat freetype jbig2dec jpeg libpng openjpeg tiff zlib tesseract leptonica

  #cd base
  libtoolize --force --copy --install
  autoconf -f -i
}

build()
{
  cd ${srcdir}/${_realname}-${pkgver}
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --disable-contrib \
    --enable-fontconfig \
    --enable-dbus \
    --enable-freetype \
    --disable-cups \
    --enable-openjpeg \
    --enable-gtk \
    --with-jbig2dec \
    --with-libiconv=gnu \
    --with-libidn \
    --with-libpaper \
    --with-system-libtiff \
    --without-x \
    --with-drivers=ALL

  make DEVICE_DEVS_EXTRA="soobj/bbox.dev" so
  #make gs.a
}

package() {
  cd ${srcdir}/${_realname}-${pkgver}
  make DESTDIR="${pkgdir}" soinstall

  #install -D -m644 "${srcdir}"/${_realname}-${pkgver}/gs.a "${pkgdir}"/${MINGW_PREFIX}/lib/libgs.a

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/${_realname}/${pkgver}/
  cp -rf "${srcdir}"/${_realname}-${pkgver}/doc "${pkgdir}${MINGW_PREFIX}"/share/${_realname}/${pkgver}/

  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE

  cp ${pkgdir}${MINGW_PREFIX}/bin/{gsc,gswin32c}.exe
  cp ${pkgdir}${MINGW_PREFIX}/bin/{gsc,gs}.exe

  # remove unwanted localized man-pages
  rm -rf "${pkgdir}${MINGW_PREFIX}"/share/man/[^man1]*
}
